﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LibraryO2S;
using BusinessObjects.BO_GetObjects;


public partial class Controls_SettingEdit : System.Web.UI.UserControl
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadSDdlGroup();
            loadData();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    private void loadData()
    {
        if (Request["s_id"] == null)
            return;
        objDT = BO_Settings.Search(Convert.ToInt32(Request["s_id"]));
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ddlGroup.SelectedValue = objDT.Rows[0]["SETTING_GROUP_ID"].ToString();
            txtDescription.Text = objDT.Rows[0]["SETTING_DESCRIPTION"].ToString();
            txtKey.Text = objDT.Rows[0]["SETTING_KEY"].ToString();
            txtName.Text = objDT.Rows[0]["SETTING_NAME"].ToString();
            txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
            txtTypeValue.Text = objDT.Rows[0]["SETTING_TYPE_VALUE"].ToString();
            txtValue.Text = objDT.Rows[0]["SETTING_VALUE"].ToString();
            ddlType.SelectedValue = objDT.Rows[0]["SETTING_TYPE"].ToString();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request["s_id"] == null)
                return;
            objDT = BO_Settings.Search(Convert.ToInt32(Request["s_id"]));
            DataTable admin = (DataTable)Session["Admin"];
            string result = BO_Settings.Update(Convert.ToInt32(Request["s_id"]), Convert.ToInt32(ddlGroup.SelectedValue), txtKey.Text.Trim(), txtName.Text.Trim(), txtDescription.Text.Trim(), txtValue.Text.Trim(), Convert.ToInt32(ddlType.SelectedValue), txtTypeValue.Text.Trim(), Convert.ToInt32(txtSortDisplay.Text), Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
            if (result.Equals("0"))
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được.");
            else
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
        }
        catch { }
        finally { }
    }
    private void loadSDdlGroup()
    {
        objDT = BO_SettingGroups.SelectAll(1);
        ddlGroup.DataSource = objDT;
        ddlGroup.DataTextField = "FULL_SETTING_NAME";
        ddlGroup.DataValueField = "SETTING_GROUP_ID";
        ddlGroup.DataBind();
    }
}